﻿using EasyCore.Configurations.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.Configurations.Extensions
{
    public static class TypeExtensions
    {
        /// <summary>
        /// 获得对象类相关配置选项设置
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static OptionsSettingAttribute GetOptionsSettings(this Type type)
        {
            var attribute = type.GetCustomAttributes(typeof(OptionsSettingAttribute), true).FirstOrDefault() as OptionsSettingAttribute; //获得指定特性
            if(attribute == null) attribute = new OptionsSettingAttribute(); //如果没有指定特性则创建默认特性
            if (string.IsNullOrWhiteSpace(attribute.Path)) {
                string name = type.Name; //获得当前类型名称
                var defaultStuffx = "Options";
                if (name.EndsWith(defaultStuffx)) name = name[0..defaultStuffx.Length]; //去掉默认后缀Options
                attribute.SetPath(name); ;
            }
            return attribute;
        }

    }
}
